export default {
  props: {
    // th 值
    thList: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      specData: [], // 规格数据
      specTable: [], // 子规格
      defaultProductNo: "bh-",
    }
  },
  computed: {
    SKUNums() {
      let num = 1
      this.specData.forEach((item, index) => {
        if (index >= 0 && item.values && item.values.length) num *= item.values.length
      })
      return num
    },
  },
  methods: {
    initData(specData, specTable) {
      this.specData = specData
      let tempArr = []
      specData.forEach(item => {
        tempArr.push({
          id: item.id,
          text: item.text,
          status: false,
        })
      })
      this.cachespecData = tempArr
      if(specTable.length > 0) {
        let temArr = []
        specTable.forEach(item => {
          // item["isUse"] = true
          const id = Date.now()
          if(!item.id) item["id"] = id
          if(item.bianhao) item["bianhao"] = this.defaultProductNo
          if(item.pic) {
            let arr = item.pic.split(",")
            let arrObj = []
            arr.forEach(it => {
              arrObj.push({ id, path: it })
            })
            item["picArr"] = arrObj
          } else {
            item["picArr"] = []
          }
          temArr.push(item)
        })
        this.specTable = temArr
      } else {
        this.specChange("add")
      }
    },
    getSpecAttr(specIndex, index) {
      const currentValues = this.specData[specIndex].values
      let indexCopy
      if (this.specData[specIndex + 1] && this.specData[specIndex + 1].values.length) {
        indexCopy = index / this.countSum(specIndex + 1)
      } else {
        indexCopy = index
      }
      const i = Math.floor(indexCopy % currentValues.length)
      if (i.toString() !== "NaN") {
        return currentValues[i].text
      } else {
        return ""
      }
    },
    countSum(specIndex) {
      let num = 1
      this.specData.forEach((item, index) => {
        if (index >= specIndex && item.values.length) {
          num *= item.values.length
        }
      })
      return num
    },
  }
}

